<!--

/*
** Copyright (c) 2015 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Qualcomm program link crash Tests</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"></script>
<script id='vshader1' type='x-shader/x-vertex'>
precision highp float;
void main() {
gl_Position = vec4( 1.0, 1.0, 1.0, 1.0 );
}
</script>
<script id='fshader1' type='x-shader/x-fragment'>
precision highp float;
uniform int renderType;
uniform sampler2D texMap;
void main() {
    vec2 uv = vec2(0.0, 0.0);
    if( renderType == 0 ) {
        gl_FragColor = texture2D( texMap, uv );
    } else {
        vec4 texture = texture2D( texMap, uv );
        gl_FragColor = texture;
    }
}
</script>

<script id='vshader2' type='x-shader/x-vertex'>
attribute vec3 vertex_position;
uniform mat4 matrix_model;
uniform mat4 matrix_viewProjection;

attribute vec4 vertex_boneWeights;
attribute vec4 vertex_boneIndices;

uniform sampler2D texture_poseMap;
uniform vec2 texture_poseMapSize;

mat4 getBoneMatrix(const in float i)
{
    float j = i * 4.0;
    float x = mod(j, float(texture_poseMapSize.x));
    float y = floor(j / float(texture_poseMapSize.x));

    float dx = 1.0 / float(texture_poseMapSize.x);
    float dy = 1.0 / float(texture_poseMapSize.y);

    y = dy * (y + 0.5);

    vec4 v1 = texture2D(texture_poseMap, vec2(dx * (x + 0.5), y));
    vec4 v2 = texture2D(texture_poseMap, vec2(dx * (x + 1.5), y));
    vec4 v3 = texture2D(texture_poseMap, vec2(dx * (x + 2.5), y));
    vec4 v4 = texture2D(texture_poseMap, vec2(dx * (x + 3.5), y));

    mat4 bone = mat4(v1, v2, v3, v4);

    return bone;
}

void main(void)
{
    mat4 modelMatrix = vertex_boneWeights.x * getBoneMatrix(vertex_boneIndices.x) +
                       vertex_boneWeights.y * getBoneMatrix(vertex_boneIndices.y) +
                       vertex_boneWeights.z * getBoneMatrix(vertex_boneIndices.z) +
                       vertex_boneWeights.w * getBoneMatrix(vertex_boneIndices.w);

    vec4 positionW = modelMatrix * vec4(vertex_position, 1.0);
    gl_Position = matrix_viewProjection * positionW;

}
</script>
<script id='fshader2' type='x-shader/x-fragment'>
precision highp float;
void main() {
    gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}
</script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test checks a known bug in some Qualcomm drivers which causes crashes when linking certain shaders. <a href='https://code.google.com/p/chromium/issues/detail?id=498947'>crbug.com/498947</a>");

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext();

gl.canvas.addEventListener("webglcontextlost", function(e) {
   testFailed("WebGL context lost");
});

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
    debug("");

    if (gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).precision == 0) {
        testPassed("highp precision not supported");
    } else {
        var program1 = wtu.setupProgram(gl, ['vshader1', 'fshader1']);
        if (!gl.getProgramParameter(program1, gl.LINK_STATUS)) {
            testFailed("Program failed to link");
        }
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");

        debug("");

        var program2 = wtu.setupProgram(gl, ['vshader2', 'fshader2']);
        if (!gl.getProgramParameter(program2, gl.LINK_STATUS)) {
            testFailed("Program failed to link");
        }
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
    }
}

// Cycle through a rAF once to give any webglcontextlost events a chance to propagate
window.requestAnimationFrame(function() { finishTest(); });

debug("");
var successfullyParsed = true;
</script>

</body>
</html>
